//
//  JMEnlargePictureView.m
//  Dome
//
//  Created by xiaorui on 15/9/16.
//  Copyright (c) 2015年 xiaorui. All rights reserved.
//

#import "JMEnlargePictureView.h"
#import "JMTapImageView.h"
#import <UIImageView+WebCache.h>
#import "JMHeader.h"


@interface JMEnlargePictureView()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation JMEnlargePictureView


- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if(self){
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Tapped:)];
        [self addGestureRecognizer:tap];
        self.clipsToBounds = YES;
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.userInteractionEnabled = YES;

        self.bgView =[[UIView alloc]init];
        self.bgView.userInteractionEnabled = YES;
        self.bgView.backgroundColor = [UIColor blackColor];
        self.bgView.alpha = 0;
        [self addSubview:self.bgView];
        
        
        self.imageView = [[UIImageView alloc]init];
        self.imageView.backgroundColor =[UIColor redColor];
        [self addSubview:self.imageView];
    }
    return self;
}

-(void)showInView:(UIView *)aView withObject:(id)sender{
    self.frame = aView.frame;
    self.hidden =NO;
    self.bgView.frame =self.frame;

    JMTapImageView *tmpView = (JMTapImageView *)sender;
    CGRect convertRect = [[tmpView superview] convertRect:tmpView.frame toView:self];
    self.imageView.frame = convertRect;
    UIImage *imgSize = tmpView.image;
    self.imageView.image = imgSize;
    
    //判断首先缩放的值
    float scaleX = self.frame.size.width/imgSize.size.width;
    float scaleY = self.frame.size.height/imgSize.size.height;
    float imgViewHeight = imgSize.size.height*scaleX;
    float imgViewWidth = imgSize.size.width*scaleX;


    if (scaleX > scaleY){
    
    }else{
    
    }
    self.bgView.alpha = 1.0;

    [UIView animateWithDuration:0.5 animations:^{

        self.imageView.frame = (CGRect){self.frame.size.width/2-imgViewWidth /2,self.frame.size.height/2-imgViewHeight/2,imgViewWidth ,imgViewHeight };
        
    } completion:^(BOOL finished) {

    }];

}




- (void) Tapped:(UIGestureRecognizer *) gesture{
    self.hidden =YES;
    self.bgView.alpha =0;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
